package com.itlong.cloud.controller.property;


import com.itlong.cloud.POJO.DTO.property.PropertyCloudServiceCastMessagePageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyCloudServiceCostMessageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.project.property.IPropertyCloudServiceCostMessageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.PageByVisitor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      物业—云服务费消息通知。
 * <desc/>
 *
 * @createDate 2019-08-07
 */
@RestController
@RequestMapping("project/cloudServiceCost")
public class PropertyCloudServiceCostMessageController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyCloudServiceCostMessageController.class);

    //消息推送
    @Autowired
    IPropertyCloudServiceCostMessageServiceClient iPropertyCloudServiceCostMessageServiceClient;


    /**
     * <desc>
     *      获取云服务费消息推送分页信息。
     * <desc/>
     *
     * @param  pageDTO 请求参数
     * @return
     * @author Juguang.S
     * @createDate 2019-08-07
     */
    @ResponseBody
    @RequestMapping(path = "/getMessageByPage" , method = RequestMethod.POST)
    public Object getMessageByPage(PropertyCloudServiceCastMessagePageDTO pageDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyCloudServiceCastMessagePageDTO.class, request);
            if(null == pageDTO || StringUtils.isEmpty(pageDTO.getProjectId()) || StringUtils.isEmpty(pageDTO.getMark().toString())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            PageByVisitor<PropertyCloudServiceCostMessageVO> voList= iPropertyCloudServiceCostMessageServiceClient.getMessageByPage(pageDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),voList);
        }catch (Exception e) {
            throw new DataAccessException("【物业_云服务费消息通知getPage】获取分页信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取云服务费消息的详细信息。
     * <desc/>
     *
     * @param  messageId 消息ID
     * @return
     * @author Juguang.S
     * @createDate 2019-08-07
     */
    @ResponseBody
    @RequestMapping(path = "/getMessageInfo" , method = RequestMethod.POST)
    public Object getMessageInfo(String messageId,String loginUserId) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            messageId = request.getAttribute("messageId") == null ? null : request.getAttribute("messageId").toString();
            loginUserId = request.getAttribute("loginUserId") == null ? null : request.getAttribute("loginUserId").toString();
            if(StringUtils.isEmpty(messageId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            PropertyCloudServiceCostMessageVO messageVO= iPropertyCloudServiceCostMessageServiceClient.getMessageInfo(messageId,loginUserId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),messageVO);
        }catch (Exception e){
            throw new DataAccessException("【物业_获取云服务费消息的详细信息】失败",e);
        }
    }

    /**
     * <desc>
     *      修改云服务费消息的状态。
     * <desc/>
     *
     * @param  messageId 消息ID 多个以逗号隔开
     * @param  type 修改类型 0删除 1修改状态为已读
     * @return
     * @author Juguang.S
     * @createDate 2019-08-07
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FINANCE_MESSAGE_STASUS_UPDATE_LOG_DESC)
    @RequestMapping(path = "/updateMessageInfo" , method = RequestMethod.POST)
    public Object updateMessageInfo(String messageId,String type) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            messageId = request.getAttribute("messageId") == null ? null : request.getAttribute("messageId").toString();
            type = request.getAttribute("type") == null ? null : request.getAttribute("type").toString();
            if(StringUtils.isEmpty(messageId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyCloudServiceCostMessageServiceClient.updateMessageInfo(messageId,type);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业_修改云服务费消息的状态】修改失败",e);
        }

    }
}
